// 页面浏览历史
var pageHistory = function() {
    var self = this;
    self.pages = [];

    // 从本地读取数据
    var v = localStorage.getItem("com.cp-zy-pages");
    if (v) {
        self.pages = JSON.parse(v);
    }
};
pageHistory.prototype = {};
pageHistory.prototype.add = function(n1, n2, id) {
    var self = this;
    var k = n1 + "_" + n2;
    for (var i in self.pages) {
        if (self.pages[i].key == k) return;
    }

    if (self.pages.indexOf(k) != -1) return;
    self.pages.push({
        key: k,
        time: new Date().getTime() / 1000,
        id: id,
    });
    if (self.pages.length > 16) {
        self.pages.splice(0, 1);
    }
    self.save();
}

pageHistory.prototype.delete = function(n1, n2) {
    var self = this;
    var k = n1 + "_" + n2;
    var index = -1;
    for (var i in self.pages) {
        if (self.pages[i].key == k) {
            index = parseInt(i);
            break;
        }
    }
    if (index == -1) return;
    self.pages.splice(index, 1);
    self.save();

    var arr = $(".page-history a");
    for (var i = 0; i < arr.length; i++) {
        var a = $(arr[i]);
        if (a.attr("n1") == n1 && a.attr("n2") == n2) {
            if (a.attr("class") && a.attr("class").indexOf("select") != -1)
            {
                a.remove();
                if (self.pages.length > 0) {
                    self.load(self.pages[0].split("_")[0], self.pages[0].split("_")[1]);
                }
                return;
            }
            a.remove();
            break;
        }
    }
}

pageHistory.prototype.draw = function(n1, n2, id) {
    var self = this;
    self.add(n1, n2, id);
    var div = $(".page-history");
    div.empty();
    for (var i in self.pages) {
        var k = self.pages[i].key;
        if (!k) continue;
        var _n1 = k.split("_")[0];
        var _n2 = k.split("_")[1];
        var a = '<a href="#" class="__c__" n1="__n1__" n2="__n2__"><span class="t1">__n2__</span>&nbsp;&nbsp;<span class="am-icon-close x"></span></a>';
        a = a.replace(/__n1__/g, _n1);
        a = a.replace(/__n2__/g, _n2);
        if (n1 == _n1 && n2 == _n2) {
            a = a.replace(/__c__/g, "select");
        }
        else {
            a = a.replace(/__c__/g, "");
        }
        div.append(a);
    }

    $(".page-history .t1").click(function(e) {
        var a = $(e.currentTarget).parent();
        self.load(a.attr("n1"), a.attr("n2"), 1);
    });
    $(".page-history .x").click(function(e) {
        var a = $(e.currentTarget).parent();
        self.delete(a.attr("n1"), a.attr("n2"));
    });
}

pageHistory.prototype.save = function() {
    var self = this;
    localStorage.setItem("com.cp-zy-pages", JSON.stringify(self.pages));
}

pageHistory.prototype.load = function(n1, n2, restore) {
    var self = this;
    var m = findMenu1(n1);
    if (m) m = findMenu2(m, n2);
    var page = 'index';
    if (m) page = "admin/" + m.page;

    urlInfo.params.n1 = n1;
    urlInfo.params.n2 = n2;
    urlInfo.params.restore = restore == 1 ? 1 : 0;

    var reload = false;
    var id;
    for (var i in self.pages) {
        if (self.pages[i].key == n1 + "_" + n2) {
            id = self.pages[i].id;
            if (new Date().getTime()/1000 - self.pages[i].time > 10 * 60) {
                // 超过10分钟了，需要重新加载
                reload = true;
            }
            self.pages[i].time = new Date().getTime()/1000;
            break;
        }
    }
    if (!id) {
        id = Util.uuid();
    }
    var frameUrl = "frame.html?restore=" + urlInfo.params.restore + "&page=" + encodeURIComponent(page) + "&id=" + id;
    if ($("#" + id).length == 0) {
        var frame = $("<iframe src='" + frameUrl + "' id='" + id + "'></iframe>");
        $(".content .operation").append(frame);
    }

    var arr = $("iframe");
    for (var i = 0; i < arr.length; i++) {
        if ($(arr[i]).attr("id") != id) $(arr[i]).hide();
        else $(arr[i]).show();
    }
    if (!restore || reload) {
        var frame = self.getFrame(id);
        frame.attr('src', frameUrl);
    }

    if (page == 'index') PageHistory.draw("index", "首页", id);
    else PageHistory.draw(n1, n2, id);

    var url = Util.genUrl({
        n1: n1,
        n2: n2,
    });
    history.pushState({
    }, "", url);

    // 设置菜单选中
    var arr = $(".left .item");
    for (var i = 0; i < arr.length; i++) {
        if ($(arr[i]).attr("value") == n1) {
            $(arr[i]).attr("class", "item active");

            var arr2 = $(arr[i]).find(".submenus a");
            for (var j = 0; j < arr2.length; j++) {
                if ($(arr2[j]).html() == n2) $(arr2[j]).attr("class", "active2");
                else $(arr2[j]).attr("class", "");
            }
        }
        else
            $(arr[i]).attr("class", "item");
    }
}

pageHistory.prototype.getFrame = function(id) {
    return $("#" + id);
}

window.PageHistory = new pageHistory();